package com.wwp.tbshop.activity;


import android.app.Fragment;
import android.app.FragmentManager;
import android.support.annotation.IdRes;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.wwp.tbshop.R;
import com.wwp.tbshop.manager.FragmentFactory;
import com.wwp.tbshop.util.SPUtils;
import com.wwp.tbshop.util.UiUtil;

import butterknife.Bind;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @Bind(R.id.rg_bottom_bar)
    public RadioGroup mRgBottomBar;
    @Bind(R.id.rb_home)
    RadioButton mRbHome;

//    private Fragment mHomeFragment;
    private FragmentManager fragmentManager;
//    private CategoryFragment mCategoryFragment;
//    private Fragment mCartFragment;
//    private Fragment mUserFragment;
//    private Fragment mMoreFragment;
    public int currentPos = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    protected void init() {
        super.init();
        //initFragment();
        mRgBottomBar.setOnCheckedChangeListener(this);
        //获取fragment管理器
        fragmentManager = getFragmentManager();
        //默认显示首页
        changePage(FragmentFactory.createFragmentByPosition(0));

    }

    /**
     * 初始化Fragment
     */
    private void initFragment() {
/*        mHomeFragment = new_goods HomeFragment();
        mCategoryFragment = new_goods CategoryFragment();
        mCartFragment = new_goods CartFragment();
        mUserFragment = new_goods UserFragment();
        mMoreFragment = new_goods MoreFragment(this);*/
    }
//    private void initFragment() {
//        mHomeFragment = new_goods HomeFragment();
//        mCategoryFragment = new_goods CategoryFragment();
//        mCartFragment = new_goods CartFragment();
//        mUserFragment = new_goods UserFragment();
//        mMoreFragment = new_goods MoreFragment();
//    }

    /**
     * 切换页面
     *
     * @param fragment
     */
    public void changePage(Fragment fragment) {
        fragmentManager
                .beginTransaction()
                .addToBackStack("")
                .replace(R.id.fl_container, fragment)
                .commitAllowingStateLoss();
    }
/*    *//**
     * 切换页面的方式(eventbus)
     * *//*
    public void changePageWithEventBus(Fragment fragment) {
        fragmentManager
                .beginTransaction()
                .addToBackStack("")
                .replace(R.id.fl_container, fragment)
                .commitAllowingStateLoss();
    }*/
    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.rb_home:
                currentPos = 0;
                changePage(FragmentFactory.createFragmentByPosition(0));
                break;
            case R.id.rb_category:
                currentPos = 1;
                changePage(FragmentFactory.createFragmentByPosition(1));
                break;
            case R.id.rb_cart:
                currentPos = 2;
                    changePage(FragmentFactory.createFragmentByPosition(2));
                break;
            case R.id.rb_user:
                currentPos = 3;
                changePage(FragmentFactory.createFragmentByPosition(3));
                break;
            case R.id.rb_more:
                currentPos = 4;
                changePage(FragmentFactory.createFragmentByPosition(4));
                break;
        }
    }

    /***
     * 回退键执行操作
     */
    @Override
    public void onBackPressed() {
        if (currentPos != 0) {
            //不是首页的话，先回首页
            currentPos = 0;
            changePage(FragmentFactory.createFragmentByPosition(0));
            mRgBottomBar.check(0);
            mRbHome.setChecked(true);
            return;
        }else{
            finish();
        }
        //        super.onBackPressed();
    }

//    long currentTime = 0;//确认退出
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//
//        if (Math.abs(System.currentTimeMillis() - currentTime) > 2000) {
//            currentTime = System.currentTimeMillis();
//            Toast.makeText(this, "再次点击,退出应用", Toast.LENGTH_SHORT).show();
//        } else {
//            this.finish();
//        }
//        return true;//保持
//    }

    //软件退出 删除缓存uerid密码
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SPUtils.putString(UiUtil.sContext,SPUtils.USERID,null);

    }
}
